package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.po.SystemMonitor;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.SystemMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SystemMonitorController {

    @Autowired
    private SystemMonitorService monitorService;

    @GetMapping("/monitor")
    public Result<SystemMonitor> getSystemInfo() {
        try {
            SystemMonitor systemInfo = monitorService.getFullSystemInfo();
            return new Result<>(systemInfo);
        } catch (Exception e) {
            log.error("获取系统信息失败", e);
            return Result.error("获取系统信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/monitor/cpu")
    public Result<SystemMonitor.CpuInfo> getCpuInfo() {
        try {
            SystemMonitor.CpuInfo cpuInfo = monitorService.getCpuInfo();
            return new Result<>(cpuInfo);
        } catch (Exception e) {
            log.error("获取CPU信息失败", e);
            return Result.error("获取CPU信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/monitor/memory")
    public Result<SystemMonitor.MemoryInfo> getMemoryInfo() {
        try {
            SystemMonitor.MemoryInfo memoryInfo = monitorService.getMemoryInfo();
            return new Result<>(memoryInfo);
        } catch (Exception e) {
            log.error("获取内存信息失败", e);
            return Result.error("获取内存信息失败: " + e.getMessage());
        }
    }

    @GetMapping("/monitor/network")
    public Result<SystemMonitor.NetworkInfo> getNetworkInfo() {
        try {
            SystemMonitor.NetworkInfo networkInfo = monitorService.getNetworkInfo();
            return new Result<>(networkInfo);
        } catch (Exception e) {
            log.error("获取网络信息失败", e);
            return Result.error("获取网络信息失败: " + e.getMessage());
        }
    }
}